﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ClientAppViaWebService.FacultiesLogicServiceRef;
using ClientAppViaWebService.Navigators;
// TODO: need buildings service

namespace ClientAppViaWebService.Forms
{
    public partial class FacultyEditForm : EducationalDivisionEditForm
    {
        private readonly Int32 ClassID = 9;
        private FacultiesLogicService lService;
        private String buildingId;

        public FacultyEditForm()
        {
            InitializeComponent();
            lService = new FacultiesLogicService();
            classTextBox.Text = ClassID.ToString();
        }

        public String Building
        {
            get { return buildingTextBox.Text; }
            set { buildingTextBox.Text = value; }
        }

        public String BuildingId
        {
            get { return buildingId; }
            set { buildingId = value; }
        }

        public String Specialities
        {
            get { return specialitiesTextBox.Text; }
            set { specialitiesTextBox.Text = value; }
        }

        protected override void SetFormProperties(Object _object)
        {
            Faculty d = (_object as Faculty);
            ID = d.ID;
            EXT_ID = d.EXT_ID;
            ParentId = d.PARENT_ID;
            Class = d.CLASS_ID;
            ObjName = d.NAME;
            BossName = d.BOSS_ID;
            DateCreated = d.DATE_CREATED;
            Description = d.DESCRIPTION;
            BuildingId = d.BUILDING;
            Specialities = d.SPECIALITIES;
            // TODO: Set building name
            Building = d.BUILDING_NAME;
        }

        protected override Object RetrieveFormProperties()
        {
            Faculty fac = new Faculty();
            fac.ID = ID;
            fac.EXT_ID = EXT_ID;
            fac.PARENT_ID = ParentId;
            fac.CLASS_ID = Class;
            fac.NAME = ObjName;
            fac.DataBaseName = DataBaseName;
            fac.BOSS_ID = BossName;
            fac.DATE_CREATED = DateCreated;
            fac.DESCRIPTION = Description;
            fac.SPECIALITIES = Specialities;
            fac.BUILDING = BuildingId;
            return fac;
        }

        protected override void LoadEditFormData(int parentNodeID)
        {
            Faculty p = lService.SelectFaculty_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }

        protected override void UpdateEntity()
        {
            Faculty obj = RetrieveFormProperties() as Faculty;
            lService.UpdateFaculty(obj);
        }

        protected override void InsertEntity()
        {
            Faculty obj = RetrieveFormProperties() as Faculty;
            lService.InsertFaculty(obj);
        }

        private void buildingButton_Click(object sender, EventArgs e)
        {
            ObjectSelectionNavigatorForm f = new ObjectSelectionNavigatorForm();
            f.LoadSearchingNavigator(BuildingId, new List<int>(new int[] { 29 }));
            if (f.ShowDialog() == DialogResult.OK)
            {
                BuildingId = f.SelectedObjectExtId;
                Building = f.SelectedObjectName;
            }
        }
    }
}
